﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Dynamic.Core.Types;

namespace Dynamic.Core.Utility
{
    /// <summary>
    /// DataType通用类：用于数据类型转化
    /// </summary>
    public class DataTypeUtility
    {
        #region 字段
        private const string _dynamicDataTypeToSqlServerDbType = "DynamicDataTypeToSqlServerDbType";
        private const string _dynamicDataTypeToCSType = "DynamicDataTypeToCSType";
        private const string _dynamicDataTypeToOracleDbType = "DynamicDataTypeToOracleDbType";
        private const string _databaseConfig = "databaseConfig";

        #endregion

        #region 方法
        /// <summary>
        /// 将DynamicDataType转化成Sql Server数据类型
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static string DynamicDataTypeToSqlDbType(DynamicDataType dataType)
        {
            string dt = dataType.ToString().ToLower();
            Dictionary<string, string> dict = ConfigUtility.GetCustomSetting(ConfigUtility.DatabaseConfigFileName, _databaseConfig, _dynamicDataTypeToSqlServerDbType);
            if (dict.ContainsKey(dt))
            {
                return dict[dt];
            }
            return string.Empty;
        }

        /// <summary>
        /// 将Sql Server数据类型转化成DynamicDataType
        /// </summary>
        /// <param name="sqlDataType"></param>
        /// <returns></returns>
        public static DynamicDataType SqlDbTypeToDynamicDataType(string sqlDataType)
        {
            Dictionary<string, string> dict = ConfigUtility.GetCustomSetting(ConfigUtility.DatabaseConfigFileName, _databaseConfig, _dynamicDataTypeToSqlServerDbType);
            string dynamicDataType = string.Empty;

            foreach (KeyValuePair<string, string> keyValue in dict)
            {
                if (string.Compare(keyValue.Value, sqlDataType, true) == 0)
                {
                    dynamicDataType = keyValue.Key;
                    break;
                }
            }

            return GetDynamicDataType(dynamicDataType);
        }

        /// <summary>
        /// 将Sql Server数据类型转化成DynamicDataType
        /// </summary>
        /// <param name="col"></param>
        /// <returns></returns>
        public static DynamicDataType SqlDbTypeToDynamicDataType(Column col)
        {
            string sqlDataType = col.DbDataType;

            if (col.IsIdentity)
            {
                return DynamicDataType.Identity;
            }
            else if (col.DbDataType.Contains(DynamicDataType.Nvarchar.ToString().ToLower()))
            {
                sqlDataType = DynamicDataType.Nvarchar.ToString().ToLower();
                if (col.MaxLength < 0)
                {
                    sqlDataType += "(max)";
                }
            }
            else if (col.DbDataType.Contains(DynamicDataType.Varchar.ToString().ToLower()))
            {
                sqlDataType = DynamicDataType.Varchar.ToString().ToLower();
                if (col.MaxLength < 0)
                {
                    sqlDataType += "(max)";
                }
            }
            else if (col.DbDataType.Contains(DynamicDataType.Varbinary.ToString().ToLower()))
            {
                sqlDataType = DynamicDataType.Varbinary.ToString().ToLower();
                if (col.MaxLength < 0)
                {
                    sqlDataType += "(max)";
                }
            }

            DynamicDataType reval = SqlDbTypeToDynamicDataType(sqlDataType);

            return reval;
        }

        /// <summary>
        /// 将DynamicDataType转化成C#数据类型
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static string DynamicDataTypeToCSType(DynamicDataType dataType)
        {
            string dt = dataType.ToString().ToLower();
            Dictionary<string, string> dict = ConfigUtility.GetCustomSetting(ConfigUtility.DatabaseConfigFileName, _databaseConfig, _dynamicDataTypeToCSType);
            if (dict.ContainsKey(dt))
            {
                return dict[dt];
            }

            return string.Empty;
        }

        /// <summary>
        /// 将DynamicDataType转化成Orcale数据类型
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static string DynamicDataTypeToOrcaleDbType(DynamicDataType dataType)
        {
            string dt = dataType.ToString().ToLower();
            Dictionary<string, string> dict = ConfigUtility.GetCustomSetting(ConfigUtility.DatabaseConfigFileName, _databaseConfig, _dynamicDataTypeToSqlServerDbType);
            if (dict.ContainsKey(dt))
            {
                return dict[dt];
            }

            return string.Empty;
        }

        /// <summary>
        /// 将Orcale数据类型转化成DynamicDataType
        /// </summary>
        /// <param name="orcaleDbType"></param>
        /// <returns></returns>
        public static DynamicDataType OrcaleDbTypeToDynamicDataType(string orcaleDbType)
        {
            Dictionary<string, string> dict = ConfigUtility.GetCustomSetting(ConfigUtility.DatabaseConfigFileName, _databaseConfig, _dynamicDataTypeToOracleDbType);
            string dynamicDataType = string.Empty;

            foreach (KeyValuePair<string, string> keyValue in dict)
            {
                if (string.Compare(keyValue.Value, orcaleDbType, true) == 0)
                {
                    break;
                }
            }

            return GetDynamicDataType(dynamicDataType);
        }

        /// <summary>
        /// 将DynamicDataType转化成Mysql数据类型
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static string DynamicDataTypeToMysqlDbType(DynamicDataType dataType)
        {
            string reval = string.Empty;

            return reval;
        }

        /// <summary>
        /// 将DynamicDataType转化成PostgreSQL数据类型
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static string DynamicDataTypeToPostgreSQLDbType(DynamicDataType dataType)
        {
            string reval = string.Empty;

            return reval;
        }

        /// <summary>
        /// 将DynamicDataType转化成SQLite数据类型
        /// </summary>
        /// <param name="dataType"></param>
        /// <returns></returns>
        public static string DynamicDataTypeToSQLiteDbType(DynamicDataType dataType)
        {
            string reval = string.Empty;

            return reval;
        }

        /// <summary>
        /// 得到DynamicDataType
        /// </summary>
        /// <param name="dynamicDataType"></param>
        /// <returns></returns>
        public static DynamicDataType GetDynamicDataType(string dynamicDataType)
        {
            return EnumUtility<DynamicDataType>.Parse(dynamicDataType);
        }

        /// <summary>
        /// 将DynamicDataType转化成相应的数据库类型
        /// </summary>
        /// <param name="dataType"></param>
        /// <param name="dbType"></param>
        /// <returns></returns>
        public static string DynamicDataTypeToDbType(DynamicDataType dataType, DatabaseType dbType)
        {
            string reval = string.Empty;

            switch (dbType)
            {
                case DatabaseType.SqlServer:
                    {
                        DynamicDataTypeToSqlDbType(dataType);
                        break;
                    }
                case DatabaseType.Oracle:
                    {
                        DynamicDataTypeToOrcaleDbType(dataType);
                        break;
                    }
            }

            return reval;
        }
        #endregion
    }
}
